/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the GPL License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guardianapis.api.client;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guardianapis.api.domain.Format;
import com.guardianapis.api.domain.jaxb.Content;
import com.guardianapis.api.domain.jaxb.Search;
import com.guardianapis.api.domain.jaxb.Tags;
import com.guardianapis.api.exceptions.ContentException;
import com.guardianapis.api.http.CommonsHttpClient;
import com.guardianapis.api.http.GuardianHttpClient;
import com.guardianapis.api.serialization.GuardianContentSerializer;
import com.guardianapis.api.serialization.JaxbContentSerializer;
import com.guardianapis.api.serialization.XStreamContentSerializer;

public class SimpleGuardianContentClientImpl extends BaseGuardianContentClient implements SimpleGuardianContentClient {

	/*******************************************************************************
	 * Constants
	 *******************************************************************************/

	protected Log log = LogFactory.getLog(SimpleGuardianContentClientImpl.class);

	/*******************************************************************************
	 * Properties
	 *******************************************************************************/

	private GuardianContentSerializer contentSerializer;
	
	/*******************************************************************************
	 * Constructors
	 *******************************************************************************/
	
	public SimpleGuardianContentClientImpl(GuardianHttpClient pHttpClient, GuardianContentSerializer pContentSerializer) {
		this(pHttpClient, pContentSerializer, GuardianConstants.DEFAULT_CONTENT_API_BASEURL, GuardianConstants.DEFAULT_CONTENT_API_KEY);
	}
	
	public SimpleGuardianContentClientImpl(GuardianHttpClient pHttpClient, GuardianContentSerializer pContentSerializer, String pApiKey) {
		this(pHttpClient, pContentSerializer, GuardianConstants.DEFAULT_CONTENT_API_BASEURL, pApiKey);
	}

	public SimpleGuardianContentClientImpl(String pApiKey) {
		// Create default implementations of the HTTP client and serializer

		super(new CommonsHttpClient(), pApiKey);
		
		try {
			// Try to create a JaxbContentSerializer
			this.contentSerializer = new JaxbContentSerializer();
		} catch (ContentException e) {
			// Could not instantiate the JaxbContentSerializer so fall back to XStream 
			log.warn("Failed to create the JaxbContentSerializer - attempting to create XStream serializer");
			this.contentSerializer = new XStreamContentSerializer();
		}
		
		this.apiBaseUrl = GuardianConstants.DEFAULT_CONTENT_API_BASEURL;
		this.apiKey = pApiKey;
	}

	public SimpleGuardianContentClientImpl(GuardianHttpClient pHttpClient, GuardianContentSerializer pContentSerializer, String pContentAPIBaseUrl, String pApiKey) {
		// Call the common basic initializer
		
		super(pHttpClient, pApiKey);
		
		// Set the specific properties defined in this constructor call
		
		this.contentSerializer = pContentSerializer;
		this.apiBaseUrl = pContentAPIBaseUrl;
	}
	
	/*******************************************************************************
	 * Implementing SimpleGuardianContentClient
	 *******************************************************************************/

	public Search search(SearchRequest pSearchRequest) throws ContentException {
		if (pSearchRequest.getFormat() != Format.XML) {
			log.warn("Forcing format to XML for unmarshalled request");
			pSearchRequest.setFormat(Format.XML);
		}
		InputStream responseStream = searchRaw(pSearchRequest);
		
		Search searchResult = this.contentSerializer.deSerializeSearch(responseStream);
		return searchResult;
	}
	
	public Content item(ItemRequest pItemRequest) throws ContentException {
		if (pItemRequest.getFormat() != Format.XML) {
			log.warn("Forcing format to XML for unmarshalled request");
			pItemRequest.setFormat(Format.XML);
		}
		InputStream responseStream = itemRaw(pItemRequest);
		Content contentResult = this.contentSerializer.deSerializeContent(responseStream);
		return contentResult;
	}

	public Tags tags(TagsRequest pTagsRequest) throws ContentException {
		if (pTagsRequest.getFormat() != Format.XML) {
			log.warn("Forcing format to XML for unmarshalled request");
			pTagsRequest.setFormat(Format.XML);
		}
		InputStream responseStream = tagsRaw(pTagsRequest);
		Tags allSubjectsResult = this.contentSerializer.deSerializeTags(responseStream);
		return allSubjectsResult;
	}
	
	/*******************************************************************************
	 * Getters / Setters
	 *******************************************************************************/

	public GuardianContentSerializer getContentSerializer() {
		return this.contentSerializer;
	}

	public void setContentSerializer(GuardianContentSerializer pContentSerializer) {
		this.contentSerializer = pContentSerializer;
	}
	
}
